{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train your first GAN model from scratch using PyTorch\n",
    "\n",
    "## 参考资料\n",
    "[1] [Train your first GAN model from scratch using PyTorch](https://blog.usejournal.com/train-your-first-gan-model-from-scratch-using-pytorch-9b72987fd2c0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torch.autograd import Variable\n",
    "from torch.utils import data as t_data\n",
    "import torchvision.datasets as datasets\n",
    "from torchvision import transforms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "0it [00:00, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./data/MNIST/raw/train-images-idx3-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████▉| 9912320/9912422 [16:03<00:00, 5966.88it/s] "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./data/MNIST/raw/train-images-idx3-ubyte.gz to ./data/MNIST/raw\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "0it [00:00, ?it/s]\u001b[A"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ./data/MNIST/raw/train-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "  0%|          | 0/28881 [00:01<?, ?it/s]\u001b[A\n",
      " 28%|██▊       | 8192/28881 [00:03<00:04, 4662.47it/s]\u001b[A\n",
      " 57%|█████▋    | 16384/28881 [00:03<00:02, 5638.84it/s]\u001b[A\n",
      " 85%|████████▌ | 24576/28881 [00:05<00:00, 5894.60it/s]\u001b[A\n",
      "32768it [00:06, 5408.42it/s]                           \u001b[A\n",
      "\n",
      "0it [00:00, ?it/s]\u001b[A\u001b[A"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./data/MNIST/raw/train-labels-idx1-ubyte.gz to ./data/MNIST/raw\n",
      "Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ./data/MNIST/raw/t10k-images-idx3-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "  0%|          | 0/1648877 [00:00<?, ?it/s]\u001b[A\u001b[A\n",
      "\n",
      "  1%|          | 16384/1648877 [00:01<00:51, 31723.22it/s]\u001b[A\u001b[A\n",
      "\n",
      "  1%|▏         | 24576/1648877 [00:01<01:10, 23146.79it/s]\u001b[A\u001b[A\n",
      "\n",
      "  2%|▏         | 40960/1648877 [00:01<00:53, 29908.77it/s]\u001b[A\u001b[A\n",
      "\n",
      "  3%|▎         | 49152/1648877 [00:02<00:51, 30890.70it/s]\u001b[A\u001b[A\n",
      "\n",
      "  4%|▍         | 65536/1648877 [00:02<00:43, 36760.50it/s]\u001b[A\u001b[A\n",
      "\n",
      "  4%|▍         | 73728/1648877 [00:02<00:44, 35577.88it/s]\u001b[A\u001b[A\n",
      "\n",
      "  5%|▍         | 81920/1648877 [00:02<00:54, 28737.21it/s]\u001b[A\u001b[A\n",
      "\n",
      "  5%|▌         | 90112/1648877 [00:03<01:05, 23675.24it/s]\u001b[A\u001b[A\n",
      "\n",
      "  6%|▋         | 106496/1648877 [00:03<00:59, 25808.69it/s]\u001b[A\u001b[A\n",
      "\n",
      "  7%|▋         | 114688/1648877 [00:04<01:00, 25549.99it/s]\u001b[A\u001b[A\n",
      "\n",
      "  7%|▋         | 122880/1648877 [00:04<01:16, 19843.56it/s]\u001b[A\u001b[A\n",
      "\n",
      "  8%|▊         | 131072/1648877 [00:05<01:14, 20441.78it/s]\u001b[A\u001b[A\n",
      "\n",
      "  8%|▊         | 139264/1648877 [00:05<01:09, 21655.54it/s]\u001b[A\u001b[A\n",
      "\n",
      "  9%|▉         | 147456/1648877 [00:06<01:33, 16114.73it/s]\u001b[A\u001b[A\n",
      "\n",
      "  9%|▉         | 155648/1648877 [00:06<01:32, 16186.64it/s]\u001b[A\u001b[A\n",
      "\n",
      " 10%|▉         | 163840/1648877 [00:08<02:50, 8689.17it/s] \u001b[A\u001b[A\n",
      "\n",
      "9920512it [16:20, 5966.88it/s]                             \u001b[A\u001b[A\n",
      "\n",
      " 11%|█         | 180224/1648877 [00:09<02:12, 11096.03it/s]\u001b[A\u001b[A\n",
      "\n",
      " 11%|█▏        | 188416/1648877 [00:10<02:25, 10066.98it/s]\u001b[A\u001b[A\n",
      "\n",
      " 12%|█▏        | 196608/1648877 [00:12<02:58, 8147.05it/s] \u001b[A\u001b[A\n",
      "\n",
      " 12%|█▏        | 204800/1648877 [00:13<03:15, 7394.31it/s]\u001b[A\u001b[A\n",
      "\n",
      " 13%|█▎        | 212992/1648877 [00:15<04:08, 5781.79it/s]\u001b[A\u001b[A\n",
      "\n",
      " 13%|█▎        | 221184/1648877 [00:17<04:00, 5932.39it/s]\u001b[A\u001b[A\n",
      "\n",
      " 14%|█▍        | 229376/1648877 [00:18<03:49, 6185.43it/s]\u001b[A\u001b[A\n",
      "32768it [00:26, 5408.42it/s]\u001b[A\n",
      "\n",
      " 14%|█▍        | 237568/1648877 [00:20<04:37, 5089.49it/s]\u001b[A\u001b[A\n",
      "\n",
      " 15%|█▍        | 245760/1648877 [00:21<04:16, 5461.72it/s]\u001b[A\u001b[A\n",
      "\n",
      " 15%|█▌        | 253952/1648877 [00:22<03:24, 6835.33it/s]\u001b[A\u001b[A\n",
      "\n",
      " 16%|█▌        | 262144/1648877 [00:23<03:25, 6757.36it/s]\u001b[A\u001b[A\n",
      "\n",
      " 16%|█▋        | 270336/1648877 [00:24<03:29, 6581.08it/s]\u001b[A\u001b[A\n",
      "\n",
      " 17%|█▋        | 278528/1648877 [00:25<03:14, 7062.68it/s]\u001b[A\u001b[A\n",
      "\n",
      " 17%|█▋        | 286720/1648877 [00:26<02:44, 8263.27it/s]\u001b[A\u001b[A\n",
      "\n",
      " 18%|█▊        | 294912/1648877 [00:29<04:27, 5054.31it/s]\u001b[A\u001b[A\n",
      "\n",
      " 18%|█▊        | 303104/1648877 [00:30<03:46, 5941.29it/s]\u001b[A\u001b[A\n",
      "\n",
      " 19%|█▉        | 311296/1648877 [00:30<02:58, 7496.89it/s]\u001b[A\u001b[A\n",
      "\n",
      " 19%|█▉        | 319488/1648877 [00:31<02:40, 8290.87it/s]\u001b[A\u001b[A\n",
      "\n",
      " 20%|█▉        | 327680/1648877 [00:32<02:37, 8385.03it/s]\u001b[A\u001b[A\n",
      "\n",
      " 20%|██        | 335872/1648877 [00:33<02:13, 9838.12it/s]\u001b[A\u001b[A\n",
      "\n",
      " 21%|██        | 344064/1648877 [00:33<02:12, 9869.21it/s]\u001b[A\u001b[A\n",
      "\n",
      " 21%|██▏       | 352256/1648877 [00:34<02:22, 9123.79it/s]\u001b[A\u001b[A\n",
      "\n",
      " 22%|██▏       | 360448/1648877 [00:36<02:47, 7688.06it/s]\u001b[A\u001b[A\n",
      "\n",
      " 22%|██▏       | 368640/1648877 [00:36<02:19, 9159.61it/s]\u001b[A\u001b[A\n",
      "\n",
      " 23%|██▎       | 376832/1648877 [00:37<02:00, 10562.18it/s]\u001b[A\u001b[A\n",
      "\n",
      " 23%|██▎       | 385024/1648877 [00:38<02:02, 10338.66it/s]\u001b[A\u001b[A\n",
      "\n",
      " 24%|██▍       | 393216/1648877 [00:38<01:51, 11285.86it/s]\u001b[A\u001b[A\n",
      "\n",
      " 24%|██▍       | 401408/1648877 [00:39<01:48, 11543.97it/s]\u001b[A\u001b[A\n",
      "\n",
      " 25%|██▍       | 409600/1648877 [00:40<01:59, 10336.32it/s]\u001b[A\u001b[A\n",
      "\n",
      " 25%|██▌       | 417792/1648877 [00:41<02:00, 10186.08it/s]\u001b[A\u001b[A\n",
      "\n",
      " 26%|██▌       | 425984/1648877 [00:42<01:59, 10195.92it/s]\u001b[A\u001b[A\n",
      "\n",
      " 26%|██▋       | 434176/1648877 [00:43<02:20, 8638.61it/s] \u001b[A\u001b[A\n",
      "\n",
      " 27%|██▋       | 442368/1648877 [00:47<04:36, 4356.08it/s]\u001b[A\u001b[A\n",
      "\n",
      " 27%|██▋       | 450560/1648877 [00:48<03:48, 5244.82it/s]\u001b[A\u001b[A\n",
      "\n",
      " 28%|██▊       | 458752/1648877 [00:49<03:22, 5882.09it/s]\u001b[A\u001b[A\n",
      "\n",
      " 28%|██▊       | 466944/1648877 [00:50<03:07, 6318.96it/s]\u001b[A\u001b[A\n",
      "\n",
      " 29%|██▉       | 475136/1648877 [00:51<02:41, 7248.21it/s]\u001b[A\u001b[A\n",
      "\n",
      " 29%|██▉       | 483328/1648877 [00:53<03:34, 5435.47it/s]\u001b[A\u001b[A\n",
      "\n",
      " 30%|██▉       | 491520/1648877 [00:56<04:29, 4290.05it/s]\u001b[A\u001b[A\n",
      "\n",
      " 30%|███       | 499712/1648877 [00:57<03:41, 5183.09it/s]\u001b[A\u001b[A\n",
      "\n",
      " 31%|███       | 507904/1648877 [00:58<03:14, 5879.35it/s]\u001b[A\u001b[A\n",
      "\n",
      " 31%|███▏      | 516096/1648877 [01:00<03:39, 5149.59it/s]\u001b[A\u001b[A\n",
      "\n",
      " 32%|███▏      | 524288/1648877 [01:01<03:26, 5433.06it/s]\u001b[A\u001b[A\n",
      "\n",
      " 32%|███▏      | 532480/1648877 [01:02<03:05, 6009.76it/s]\u001b[A\u001b[A\n",
      "\n",
      " 33%|███▎      | 540672/1648877 [01:03<03:10, 5814.02it/s]\u001b[A\u001b[A\n",
      "\n",
      " 33%|███▎      | 548864/1648877 [01:05<03:01, 6045.50it/s]\u001b[A\u001b[A\n",
      "\n",
      " 34%|███▍      | 557056/1648877 [01:06<03:05, 5896.13it/s]\u001b[A\u001b[A\n",
      "\n",
      " 34%|███▍      | 565248/1648877 [01:08<03:06, 5808.95it/s]\u001b[A\u001b[A\n",
      "\n",
      " 35%|███▍      | 573440/1648877 [01:08<02:32, 7056.41it/s]\u001b[A\u001b[A\n",
      "\n",
      " 35%|███▌      | 581632/1648877 [01:09<02:18, 7721.41it/s]\u001b[A\u001b[A\n",
      "\n",
      " 36%|███▌      | 589824/1648877 [01:10<02:18, 7634.95it/s]\u001b[A\u001b[A\n",
      "\n",
      " 36%|███▋      | 598016/1648877 [01:10<01:45, 9928.40it/s]\u001b[A\u001b[A\n",
      "\n",
      " 37%|███▋      | 606208/1648877 [01:11<01:39, 10480.35it/s]\u001b[A\u001b[A\n",
      "\n",
      " 37%|███▋      | 614400/1648877 [01:12<01:37, 10591.55it/s]\u001b[A\u001b[A\n",
      "\n",
      " 38%|███▊      | 622592/1648877 [01:12<01:28, 11611.42it/s]\u001b[A\u001b[A\n",
      "\n",
      " 38%|███▊      | 630784/1648877 [01:13<01:11, 14223.23it/s]\u001b[A\u001b[A\n",
      "\n",
      " 39%|███▉      | 638976/1648877 [01:13<01:20, 12620.51it/s]\u001b[A\u001b[A\n",
      "\n",
      " 39%|███▉      | 647168/1648877 [01:15<01:42, 9768.09it/s] \u001b[A\u001b[A\n",
      "\n",
      " 40%|███▉      | 655360/1648877 [01:16<02:16, 7260.79it/s]\u001b[A\u001b[A\n",
      "\n",
      " 40%|████      | 663552/1648877 [01:18<02:37, 6238.44it/s]\u001b[A\u001b[A\n",
      "\n",
      " 41%|████      | 671744/1648877 [01:19<02:25, 6724.11it/s]\u001b[A\u001b[A\n",
      "\n",
      " 41%|████      | 679936/1648877 [01:20<01:58, 8196.06it/s]\u001b[A\u001b[A\n",
      "\n",
      " 42%|████▏     | 688128/1648877 [01:21<02:01, 7897.55it/s]\u001b[A\u001b[A\n",
      "\n",
      " 42%|████▏     | 696320/1648877 [01:21<01:45, 9043.34it/s]\u001b[A\u001b[A\n",
      "\n",
      " 43%|████▎     | 704512/1648877 [01:24<02:28, 6370.66it/s]\u001b[A\u001b[A\n",
      "\n",
      " 43%|████▎     | 712704/1648877 [01:24<02:05, 7438.89it/s]\u001b[A\u001b[A\n",
      "\n",
      " 44%|████▎     | 720896/1648877 [01:26<02:26, 6354.74it/s]\u001b[A\u001b[A\n",
      "\n",
      " 44%|████▍     | 729088/1648877 [01:27<02:17, 6703.95it/s]\u001b[A\u001b[A\n",
      "\n",
      " 45%|████▍     | 737280/1648877 [01:28<02:11, 6943.52it/s]\u001b[A\u001b[A\n",
      "\n",
      " 45%|████▌     | 745472/1648877 [01:29<01:55, 7790.72it/s]\u001b[A\u001b[A\n",
      "\n",
      " 46%|████▌     | 753664/1648877 [01:29<01:38, 9132.88it/s]\u001b[A\u001b[A\n",
      "\n",
      " 46%|████▌     | 761856/1648877 [01:32<02:30, 5901.01it/s]\u001b[A\u001b[A\n",
      "\n",
      " 47%|████▋     | 770048/1648877 [01:33<02:09, 6804.24it/s]\u001b[A\u001b[A\n",
      "\n",
      " 47%|████▋     | 778240/1648877 [01:34<02:04, 6982.66it/s]\u001b[A\u001b[A\n",
      "\n",
      " 48%|████▊     | 786432/1648877 [01:35<02:05, 6885.97it/s]\u001b[A\u001b[A\n",
      "\n",
      " 48%|████▊     | 794624/1648877 [01:36<01:42, 8334.88it/s]\u001b[A\u001b[A\n",
      "\n",
      " 49%|████▊     | 802816/1648877 [01:37<02:04, 6777.89it/s]\u001b[A\u001b[A\n",
      "\n",
      " 49%|████▉     | 811008/1648877 [01:39<02:06, 6636.46it/s]\u001b[A\u001b[A\n",
      "\n",
      " 50%|████▉     | 819200/1648877 [01:40<02:14, 6171.27it/s]\u001b[A\u001b[A\n",
      "\n",
      " 50%|█████     | 827392/1648877 [01:41<01:48, 7599.86it/s]\u001b[A\u001b[A\n",
      "\n",
      " 51%|█████     | 835584/1648877 [01:42<01:44, 7776.95it/s]\u001b[A\u001b[A\n",
      "\n",
      " 51%|█████     | 843776/1648877 [01:42<01:36, 8328.97it/s]\u001b[A\u001b[A\n",
      "\n",
      " 52%|█████▏    | 851968/1648877 [01:43<01:29, 8942.80it/s]\u001b[A\u001b[A\n",
      "\n",
      " 52%|█████▏    | 860160/1648877 [01:43<01:08, 11456.84it/s]\u001b[A\u001b[A\n",
      "\n",
      " 53%|█████▎    | 868352/1648877 [01:44<01:02, 12495.77it/s]\u001b[A\u001b[A\n",
      "\n",
      " 53%|█████▎    | 876544/1648877 [01:45<00:59, 12989.82it/s]\u001b[A\u001b[A\n",
      "\n",
      " 54%|█████▎    | 884736/1648877 [01:45<00:46, 16456.04it/s]\u001b[A\u001b[A\n",
      "\n",
      " 54%|█████▍    | 892928/1648877 [01:46<01:02, 12139.76it/s]\u001b[A\u001b[A\n",
      "\n",
      " 55%|█████▍    | 901120/1648877 [01:46<00:50, 14928.48it/s]\u001b[A\u001b[A\n",
      "\n",
      " 55%|█████▌    | 909312/1648877 [01:47<00:55, 13327.13it/s]\u001b[A\u001b[A\n",
      "\n",
      " 56%|█████▌    | 917504/1648877 [01:47<00:51, 14093.05it/s]\u001b[A\u001b[A\n",
      "\n",
      " 56%|█████▌    | 925696/1648877 [01:48<00:45, 16055.87it/s]\u001b[A\u001b[A\n",
      "\n",
      " 57%|█████▋    | 933888/1648877 [01:48<00:44, 16224.91it/s]\u001b[A\u001b[A\n",
      "\n",
      " 57%|█████▋    | 942080/1648877 [01:49<01:03, 11201.19it/s]\u001b[A\u001b[A\n",
      "\n",
      " 58%|█████▊    | 950272/1648877 [01:51<01:26, 8065.47it/s] \u001b[A\u001b[A\n",
      "\n",
      " 58%|█████▊    | 958464/1648877 [01:52<01:25, 8106.92it/s]\u001b[A\u001b[A\n",
      "\n",
      " 59%|█████▊    | 966656/1648877 [01:54<01:42, 6639.67it/s]\u001b[A\u001b[A\n",
      "\n",
      " 59%|█████▉    | 974848/1648877 [01:55<01:39, 6762.44it/s]\u001b[A\u001b[A\n",
      "\n",
      " 60%|█████▉    | 983040/1648877 [01:59<02:43, 4062.47it/s]\u001b[A\u001b[A\n",
      "\n",
      " 60%|██████    | 991232/1648877 [02:00<02:11, 5002.21it/s]\u001b[A\u001b[A\n",
      "\n",
      " 61%|██████    | 999424/1648877 [02:00<01:42, 6321.98it/s]\u001b[A\u001b[A\n",
      "\n",
      " 61%|██████    | 1007616/1648877 [02:01<01:25, 7462.97it/s]\u001b[A\u001b[A\n",
      "\n",
      " 62%|██████▏   | 1015808/1648877 [02:02<01:24, 7474.35it/s]\u001b[A\u001b[A\n",
      "\n",
      " 62%|██████▏   | 1024000/1648877 [02:03<01:22, 7532.90it/s]\u001b[A\u001b[A\n",
      "\n",
      " 63%|██████▎   | 1032192/1648877 [02:04<01:08, 8981.37it/s]\u001b[A\u001b[A\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 63%|██████▎   | 1040384/1648877 [02:05<01:10, 8683.22it/s]\u001b[A\u001b[A\n",
      "\n",
      " 64%|██████▎   | 1048576/1648877 [02:05<00:56, 10646.33it/s]\u001b[A\u001b[A\n",
      "\n",
      " 64%|██████▍   | 1056768/1648877 [02:06<01:00, 9779.64it/s] \u001b[A\u001b[A\n",
      "\n",
      " 65%|██████▍   | 1064960/1648877 [02:06<00:52, 11154.72it/s]\u001b[A\u001b[A\n",
      "\n",
      " 65%|██████▌   | 1073152/1648877 [02:07<00:56, 10133.80it/s]\u001b[A\u001b[A\n",
      "\n",
      " 66%|██████▌   | 1081344/1648877 [02:08<00:46, 12322.99it/s]\u001b[A\u001b[A\n",
      "\n",
      " 66%|██████▌   | 1089536/1648877 [02:09<00:57, 9797.48it/s] \u001b[A\u001b[A\n",
      "\n",
      " 67%|██████▋   | 1097728/1648877 [02:10<00:56, 9698.69it/s]\u001b[A\u001b[A\n",
      "\n",
      " 67%|██████▋   | 1105920/1648877 [02:11<00:58, 9225.28it/s]\u001b[A\u001b[A\n",
      "\n",
      " 68%|██████▊   | 1114112/1648877 [02:11<00:50, 10620.86it/s]\u001b[A\u001b[A\n",
      "\n",
      " 68%|██████▊   | 1122304/1648877 [02:14<01:23, 6318.54it/s] \u001b[A\u001b[A\n",
      "\n",
      " 69%|██████▊   | 1130496/1648877 [02:14<01:06, 7754.60it/s]\u001b[A\u001b[A\n",
      "\n",
      " 69%|██████▉   | 1138688/1648877 [02:15<00:55, 9209.56it/s]\u001b[A\u001b[A\n",
      "\n",
      " 70%|██████▉   | 1146880/1648877 [02:15<00:47, 10604.32it/s]\u001b[A\u001b[A\n",
      "\n",
      " 70%|███████   | 1155072/1648877 [02:17<00:54, 9012.03it/s] \u001b[A\u001b[A\n",
      "\n",
      " 71%|███████   | 1163264/1648877 [02:17<00:42, 11514.81it/s]\u001b[A\u001b[A\n",
      "\n",
      " 71%|███████   | 1171456/1648877 [02:18<00:46, 10314.36it/s]\u001b[A\u001b[A\n",
      "\n",
      " 72%|███████▏  | 1179648/1648877 [02:19<00:54, 8630.94it/s] \u001b[A\u001b[A\n",
      "\n",
      " 72%|███████▏  | 1187840/1648877 [02:20<00:51, 9007.97it/s]\u001b[A\u001b[A\n",
      "\n",
      " 73%|███████▎  | 1196032/1648877 [02:20<00:43, 10436.77it/s]\u001b[A\u001b[A\n",
      "\n",
      " 73%|███████▎  | 1204224/1648877 [02:21<00:38, 11692.73it/s]\u001b[A\u001b[A\n",
      "\n",
      " 74%|███████▎  | 1212416/1648877 [02:21<00:34, 12758.51it/s]\u001b[A\u001b[A\n",
      "\n",
      " 74%|███████▍  | 1220608/1648877 [02:22<00:32, 13187.31it/s]\u001b[A\u001b[A\n",
      "\n",
      " 75%|███████▍  | 1228800/1648877 [02:22<00:26, 16114.72it/s]\u001b[A\u001b[A\n",
      "\n",
      " 75%|███████▌  | 1236992/1648877 [02:23<00:28, 14208.37it/s]\u001b[A\u001b[A\n",
      "\n",
      " 76%|███████▌  | 1245184/1648877 [02:24<00:29, 13611.50it/s]\u001b[A\u001b[A\n",
      "\n",
      " 76%|███████▌  | 1253376/1648877 [02:24<00:33, 11860.08it/s]\u001b[A\u001b[A\n",
      "\n",
      " 77%|███████▋  | 1261568/1648877 [02:25<00:33, 11601.72it/s]\u001b[A\u001b[A\n",
      "\n",
      " 77%|███████▋  | 1269760/1648877 [02:26<00:39, 9488.69it/s] \u001b[A\u001b[A\n",
      "\n",
      " 78%|███████▊  | 1277952/1648877 [02:27<00:37, 9891.35it/s]\u001b[A\u001b[A\n",
      "\n",
      " 78%|███████▊  | 1286144/1648877 [02:28<00:39, 9218.61it/s]\u001b[A\u001b[A\n",
      "\n",
      " 78%|███████▊  | 1294336/1648877 [02:29<00:42, 8299.56it/s]\u001b[A\u001b[A\n",
      "\n",
      " 79%|███████▉  | 1302528/1648877 [02:30<00:35, 9754.30it/s]\u001b[A\u001b[A\n",
      "\n",
      " 79%|███████▉  | 1310720/1648877 [02:30<00:30, 11119.98it/s]\u001b[A\u001b[A\n",
      "\n",
      " 80%|███████▉  | 1318912/1648877 [02:31<00:29, 11054.64it/s]\u001b[A\u001b[A\n",
      "\n",
      " 80%|████████  | 1327104/1648877 [02:32<00:24, 13291.59it/s]\u001b[A\u001b[A\n",
      "\n",
      " 81%|████████  | 1335296/1648877 [02:33<00:28, 11195.05it/s]\u001b[A\u001b[A\n",
      "\n",
      " 81%|████████▏ | 1343488/1648877 [02:33<00:25, 11993.18it/s]\u001b[A\u001b[A\n",
      "\n",
      " 82%|████████▏ | 1351680/1648877 [02:34<00:29, 10207.78it/s]\u001b[A\u001b[A\n",
      "\n",
      " 82%|████████▏ | 1359872/1648877 [02:36<00:34, 8315.56it/s] \u001b[A\u001b[A\n",
      "\n",
      " 83%|████████▎ | 1368064/1648877 [02:36<00:28, 9752.69it/s]\u001b[A\u001b[A\n",
      "\n",
      " 83%|████████▎ | 1376256/1648877 [02:37<00:24, 11100.43it/s]\u001b[A\u001b[A\n",
      "\n",
      " 84%|████████▍ | 1384448/1648877 [02:37<00:19, 13798.17it/s]\u001b[A\u001b[A\n",
      "\n",
      " 84%|████████▍ | 1392640/1648877 [02:38<00:22, 11216.86it/s]\u001b[A\u001b[A\n",
      "\n",
      " 85%|████████▍ | 1400832/1648877 [02:39<00:22, 11200.44it/s]\u001b[A\u001b[A\n",
      "\n",
      " 85%|████████▌ | 1409024/1648877 [02:39<00:22, 10815.50it/s]\u001b[A\u001b[A\n",
      "\n",
      " 86%|████████▌ | 1417216/1648877 [02:42<00:33, 6966.97it/s] \u001b[A\u001b[A\n",
      "\n",
      " 86%|████████▋ | 1425408/1648877 [02:42<00:28, 7823.68it/s]\u001b[A\u001b[A\n",
      "\n",
      " 87%|████████▋ | 1433600/1648877 [02:43<00:23, 9275.56it/s]\u001b[A\u001b[A\n",
      "\n",
      " 87%|████████▋ | 1441792/1648877 [02:43<00:19, 10658.87it/s]\u001b[A\u001b[A\n",
      "\n",
      " 88%|████████▊ | 1449984/1648877 [02:44<00:20, 9559.76it/s] \u001b[A\u001b[A\n",
      "\n",
      " 88%|████████▊ | 1458176/1648877 [02:46<00:25, 7600.10it/s]\u001b[A\u001b[A\n",
      "\n",
      " 89%|████████▉ | 1466368/1648877 [02:47<00:20, 9049.92it/s]\u001b[A\u001b[A\n",
      "\n",
      " 89%|████████▉ | 1474560/1648877 [02:47<00:16, 10479.25it/s]\u001b[A\u001b[A\n",
      "\n",
      " 90%|████████▉ | 1482752/1648877 [02:48<00:14, 11421.59it/s]\u001b[A\u001b[A\n",
      "\n",
      " 90%|█████████ | 1490944/1648877 [02:48<00:12, 12792.16it/s]\u001b[A\u001b[A\n",
      "\n",
      " 91%|█████████ | 1499136/1648877 [02:49<00:12, 12321.32it/s]\u001b[A\u001b[A\n",
      "\n",
      " 91%|█████████▏| 1507328/1648877 [02:50<00:13, 10420.72it/s]\u001b[A\u001b[A\n",
      "\n",
      " 92%|█████████▏| 1515520/1648877 [02:50<00:11, 11713.82it/s]\u001b[A\u001b[A\n",
      "\n",
      " 92%|█████████▏| 1523712/1648877 [02:51<00:10, 11510.25it/s]\u001b[A\u001b[A\n",
      "\n",
      " 93%|█████████▎| 1531904/1648877 [02:52<00:10, 11051.33it/s]\u001b[A\u001b[A\n",
      "\n",
      " 93%|█████████▎| 1540096/1648877 [02:52<00:08, 12253.71it/s]\u001b[A\u001b[A\n",
      "\n",
      " 94%|█████████▍| 1548288/1648877 [02:53<00:07, 13288.15it/s]\u001b[A\u001b[A\n",
      "\n",
      " 94%|█████████▍| 1556480/1648877 [02:54<00:07, 12504.27it/s]\u001b[A\u001b[A\n",
      "\n",
      " 95%|█████████▍| 1564672/1648877 [02:55<00:09, 9142.24it/s] \u001b[A\u001b[A\n",
      "\n",
      " 95%|█████████▌| 1572864/1648877 [02:56<00:07, 9653.63it/s]\u001b[A\u001b[A\n",
      "\n",
      " 96%|█████████▌| 1581056/1648877 [02:56<00:05, 12238.75it/s]\u001b[A\u001b[A\n",
      "\n",
      " 96%|█████████▋| 1589248/1648877 [02:57<00:05, 11510.25it/s]\u001b[A\u001b[A\n",
      "\n",
      " 97%|█████████▋| 1597440/1648877 [02:57<00:04, 12637.26it/s]\u001b[A\u001b[A\n",
      "\n",
      " 97%|█████████▋| 1605632/1648877 [02:58<00:03, 11730.22it/s]\u001b[A\u001b[A\n",
      "\n",
      " 98%|█████████▊| 1613824/1648877 [02:59<00:02, 12821.74it/s]\u001b[A\u001b[A\n",
      "\n",
      " 98%|█████████▊| 1622016/1648877 [02:59<00:02, 12241.54it/s]\u001b[A\u001b[A\n",
      "\n",
      " 99%|█████████▉| 1630208/1648877 [03:00<00:01, 10674.74it/s]\u001b[A\u001b[A\n",
      "\n",
      " 99%|█████████▉| 1638400/1648877 [03:01<00:00, 11229.60it/s]\u001b[A\u001b[A\n",
      "\n",
      "100%|█████████▉| 1646592/1648877 [03:01<00:00, 13988.53it/s]\u001b[A\u001b[A\n",
      "\n",
      "\n",
      "0it [00:00, ?it/s]\u001b[A\u001b[A\u001b[A"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./data/MNIST/raw/t10k-images-idx3-ubyte.gz to ./data/MNIST/raw\n",
      "Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./data/MNIST/raw/t10k-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "8192it [00:01, 7773.41it/s]             \u001b[A\u001b[A\u001b[A"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./data/MNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/MNIST/raw\n",
      "Processing...\n",
      "Done!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "data_transforms = transforms.Compose([transforms.ToTensor()])\n",
    "mnist_trainset = datasets.MNIST(root='./data', train=True,    \n",
    "                           download=True, transform=data_transforms)\n",
    "batch_size=4\n",
    "dataloader_mnist_train = t_data.DataLoader(mnist_trainset, \n",
    "                                           batch_size=batch_size,\n",
    "                                           shuffle=True\n",
    "                                           )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_some_noise():\n",
    "    return torch.rand(batch_size,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# defining generator class\n",
    "\n",
    "class generator(nn.Module):\n",
    "    \n",
    "    def __init__(self, inp, out):\n",
    "        \n",
    "        super(generator, self).__init__()\n",
    "        \n",
    "        self.net = nn.Sequential(\n",
    "                                 nn.Linear(inp,300),\n",
    "                                 nn.ReLU(inplace=True),\n",
    "                                 nn.Linear(300,1000),\n",
    "                                 nn.ReLU(inplace=True),\n",
    "                                 nn.Linear(1000,800),\n",
    "                                 nn.ReLU(inplace=True),\n",
    "                                 nn.Linear(800,out)\n",
    "                                    )\n",
    "        \n",
    "    def forward(self, x):\n",
    "        x = self.net(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# defining discriminator class\n",
    "\n",
    "class discriminator(nn.Module):\n",
    "    \n",
    "    def __init__(self, inp, out):\n",
    "        \n",
    "        super(discriminator, self).__init__()\n",
    "        \n",
    "        self.net = nn.Sequential(\n",
    "                                 nn.Linear(inp,300),\n",
    "                                 nn.ReLU(inplace=True),\n",
    "                                 nn.Linear(300,300),\n",
    "                                 nn.ReLU(inplace=True),\n",
    "                                 nn.Linear(300,200),\n",
    "                                 nn.ReLU(inplace=True),\n",
    "                                 nn.Linear(200,out),\n",
    "                                 nn.Sigmoid()\n",
    "                                    )\n",
    "        \n",
    "    def forward(self, x):\n",
    "        x = self.net(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_img(array,number=None):\n",
    "    array = array.detach()\n",
    "    array = array.reshape(28,28)\n",
    "    \n",
    "    plt.imshow(array,cmap='binary')\n",
    "    plt.xticks([])\n",
    "    plt.yticks([])\n",
    "    if number:\n",
    "        plt.xlabel(number,fontsize='x-large')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'gen' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-a7205884ce6a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0mcriteriond2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mBCELoss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0moptimizerd2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0moptim\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSGD\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgen\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.001\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmomentum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0mprinting_steps\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m200\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'gen' is not defined"
     ]
    }
   ],
   "source": [
    "d_steps = 100\n",
    "g_steps = 100\n",
    "\n",
    "# criteriond1 = nn.BCELoss()\n",
    "# optimizerd1 = optim.SGD(dis.parameters(), lr=0.001, momentum=0.9)\n",
    "\n",
    "criteriond2 = nn.BCELoss()\n",
    "optimizerd2 = optim.SGD(gen.parameters(), lr=0.001, momentum=0.9)\n",
    "\n",
    "printing_steps = 200\n",
    "\n",
    "epochs = 50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for epoch in range(epochs):\n",
    "    \n",
    "    print epoch\n",
    "\n",
    "    # training discriminator\n",
    "    for d_step in range(d_steps):\n",
    "        dis.zero_grad()\n",
    "        \n",
    "        # training discriminator on real data\n",
    "        for inp_real,_ in dataloader_mnist_train:\n",
    "            inp_real_x = inp_real\n",
    "            break\n",
    "\n",
    "        inp_real_x = inp_real_x.reshape(batch_size,784)\n",
    "        dis_real_out = dis(inp_real_x)\n",
    "        dis_real_loss = criteriond1(dis_real_out,\n",
    "                              Variable(torch.ones(batch_size,1)))\n",
    "        dis_real_loss.backward()\n",
    "\n",
    "        # training discriminator on data produced by generator\n",
    "        inp_fake_x_gen = make_some_noise()\n",
    "        #output from generator is generated        \n",
    "        dis_inp_fake_x = gen(inp_fake_x_gen).detach()\n",
    "        dis_fake_out = dis(dis_inp_fake_x)\n",
    "        dis_fake_loss = criteriond1(dis_fake_out,\n",
    "                                Variable(torch.zeros(batch_size,1)))\n",
    "        dis_fake_loss.backward()\n",
    "\n",
    "        optimizerd1.step()\n",
    "        \n",
    "        \n",
    "            \n",
    "    # training generator\n",
    "    for g_step in range(g_steps):\n",
    "        gen.zero_grad()\n",
    "        \n",
    "        #generating data for input for generator\n",
    "        gen_inp = make_some_noise()\n",
    "        \n",
    "        gen_out = gen(gen_inp)\n",
    "        dis_out_gen_training = dis(gen_out)\n",
    "        gen_loss = criteriond2(dis_out_gen_training,\n",
    "                               Variable(torch.ones(batch_size,1)))\n",
    "        gen_loss.backward()\n",
    "        \n",
    "        optimizerd2.step()\n",
    "        \n",
    "    if epoch%printing_steps==0:\n",
    "        plot_img(gen_out[0])\n",
    "        plot_img(gen_out[1])\n",
    "        plot_img(gen_out[2])\n",
    "        plot_img(gen_out[3])\n",
    "        print(\"\\n\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1654784it [03:19, 13988.53it/s]                             \u001b[A\u001b[A"
     ]
    }
   ],
   "source": [
    "plot_img(gen(make_some_noise()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit ('base': conda)",
   "language": "python",
   "name": "python37464bitbaseconda5073072be3c4479bad20c57a46be46a1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
